/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.service.impl;

import com.wteam.modules.system.service.CreditTypeService;
import com.wteam.modules.system.domain.CreditType;
import com.wteam.modules.system.domain.dto.CreditTypeDTO;
import com.wteam.modules.system.domain.criteria.CreditTypeQueryCriteria;
import com.wteam.modules.system.domain.mapper.CreditTypeMapper;
import com.wteam.modules.system.repository.CreditTypeRepository;
import com.wteam.exception.BadRequestException;
import com.wteam.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 学分类别 业务实现层.
 *
 * @author herry_wong
 * @since 2020-12-29
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "creditType")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CreditTypeServiceImpl implements CreditTypeService {

    private final CreditTypeRepository creditTypeRepository;

    private final CreditTypeMapper creditTypeMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(CreditTypeQueryCriteria criteria, Pageable pageable){
        Page<CreditType> page = creditTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(creditTypeMapper::toDto));
    }

    @Override
    public List<CreditTypeDTO> queryAll(CreditTypeQueryCriteria criteria){
        return creditTypeMapper.toDto(creditTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
//    @Cacheable(key = "'id:' + #p0")
    public CreditTypeDTO findDTOById(Long id) {
        CreditType creditType = creditTypeRepository.findById(id).orElse(null);
        ValidUtil.notNull(creditType, CreditType.ENTITY_NAME, "id", id);
        return creditTypeMapper.toDto(creditType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CreditTypeDTO create(CreditType resources) {
        return creditTypeMapper.toDto(creditTypeRepository.save(resources));
    }

    @Override
    @CacheEvict(key = "'id:' + #p0.id")
    @Transactional(rollbackFor = Exception.class)
    public void update(CreditType resources) {
        CreditType creditType = creditTypeRepository.findById(resources.getId()).orElse(null);
        ValidUtil.notNull(creditType, CreditType.ENTITY_NAME, "id",resources.getId());

        creditType.copy(resources);
        creditTypeRepository.save(creditType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Set<Long> ids) {
        redisUtils.delByKeys("creditType::id:", ids);
        creditTypeRepository.logicDeleteInBatchById(ids);
    }

    @Override
    public void download(List<CreditTypeDTO> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CreditTypeDTO creditType : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("类型名称", creditType.getName());
            map.put("学分说明", creditType.getDetail());
            map.put("创建时间", creditType.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}